package com.john.server.exception;

import com.john.server.pojo.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理
 * 日期 2021/6/7 17:09
 * 创建者：john driver
 */
@RestControllerAdvice
@Slf4j
public class GlobalException {
    @ExceptionHandler(SQLException.class)
    public RespBean mysqlException(SQLException e){
        log.info("数据库异常信息："+e.getMessage());
        if(e instanceof SQLIntegrityConstraintViolationException){
            return RespBean.error("该数据有关联数据，操作失败");
        }
        return RespBean.error("数据库异常，操作失败！");
    }

}
